#!/bin/bash
if [[ $(whoami) != "lfs" ]]; then
	echo "Please log in as user lfs...";
	exit 1;
fi
cd $LFS/sources/pkg || exit 1
source ./utils.sh || exit 2
#######################################
# set package name here
PACKAGE=$(echo $0 | sed -e 's/.*make_\(.*\).sh/\1/');
#######################################
PKG_NAME=$(pkgname tools $PACKAGE) || exit 1;
PKG_VERSION=$(pkgversion tools $PACKAGE) || exit 2;
PKG_ARCHIVE=$(pkgarchive tools $PACKAGE) || exit 3;
echo "make $PACKAGE ($PKG_NAME) with version $PKG_VERSION from archive $PKG_ARCHIVE"
./unpack.sh $PKG_ARCHIVE || exit 4;
cd ../$PKG_ARCHIVE || (errcho "Can not change into directory ../$PKG_ARCHIVE" && exit 5);
#######################################
# prepare package source (patch etc.)
cat gcc/limitx.h gcc/glimits.h gcc/limity.h > \
  `dirname $($LFS_TGT-gcc -print-libgcc-file-name)`/include-fixed/limits.h && \
for file in $(find gcc/config -name linux64.h -o -name linux.h -o -name sysv4.h); do
  cp -uv $file{,.orig} && \
  sed -e 's@/lib\(64\)\?\(32\)\?/ld@/tools&@g' \
      -e 's@/usr@/tools@g' $file.orig > $file && \
  echo '
#undef STANDARD_STARTFILE_PREFIX_1
#undef STANDARD_STARTFILE_PREFIX_2
#define STANDARD_STARTFILE_PREFIX_1 "/tools/lib/"
#define STANDARD_STARTFILE_PREFIX_2 ""' >> $file && \
  touch $file.orig;
done && \
tar -xf ../mpfr-3.1.2.tar.xz && \
mv -v mpfr-3.1.2 mpfr && \
tar -xf ../gmp-6.0.0a.tar.xz && \
mv -v gmp-6.0.0 gmp && \
tar -xf ../mpc-1.0.2.tar.gz && \
mv -v mpc-1.0.2 mpc && \
sed -i 's/if \((code.*))\)/if (\1 \&\& \!DEBUG_INSN_P (insn))/' gcc/sched-deps.c && \
rm -Rfv ../gcc-build && \
mkdir -v ../gcc-build && \
cd ../gcc-build


#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package preparing with code $ERR";
    exit 6;
fi
#######################################
# configure and make package
CC=$LFS_TGT-gcc                                      \
CXX=$LFS_TGT-g++                                     \
AR=$LFS_TGT-ar                                       \
RANLIB=$LFS_TGT-ranlib                               \
../$PKG_ARCHIVE/configure                               \
    --prefix=/tools                                  \
    --with-local-prefix=/tools                       \
    --with-native-system-header-dir=/tools/include   \
    --enable-languages=c,c++                         \
    --disable-libstdcxx-pch                          \
    --disable-multilib                               \
    --disable-bootstrap                              \
    --disable-libgomp \
    && \
    make && \
    make install



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 6;
fi
#######################################
# make post checks and configuration
ln -sfv gcc /tools/bin/cc && \
echo 'main(){}' > dummy.c && \
cc dummy.c && \
ELF=$(readelf -l a.out | grep ': /tools') && \
if [[ $ELF == "" ]]; then
    errcho "Wrong linker path... $(readelf -l a.out | grep 'interpreter')";
    exit 10;
else
    echo "readelf result: $ELF";
fi && \
rm -v dummy.c a.out && \
cd .. && rm -Rfv gcc-build



#######################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package building with code $ERR";
    exit 7;
fi
cd $LFS/sources && \
rm -Rfv $PKG_ARCHIVE || (errcho "Can not remove directory $PKG_ARCHIVE" && exit 8);
echo "Installed $PACKAGE"
echo " --OK--"
